<?php

class DefaultController extends Controller
{
    public function accessRules() {
        return array(
        );
    }
	public function actionIndex()
	{
		$this->render('index');
	}
	
	public function actionList()
	{
		//$this->render('index');
		Yii::import('Business.models.Service');
		Yii::import('application.behaviors.CArJSON');
		$json = new CArJSON();
		$relations = array('service');
		//TODO: hard code here.
		$businessId=1;
		$sidCriteria=new CDbCriteria();
		$sidCriteria->compare('business_id',$businessId);
		$services=Service::model()->findAll($sidCriteria);
		$serviceIds=CHtml::listData($services,'id','id');
		//Fill all service has business id is the current business id.
		$criteria=new CDbCriteria();
        $criteria->order="id DESC";
        $criteria->addInCondition("service_id",array_values($serviceIds));		
        $models = Appointment::model()->findAll($criteria);
		$this->sendResponse(200, $json->toJSON($models, $relations));
	}

    public function actionCreate()
    {
        $data = CJSON::decode(file_get_contents('php://input'));
        $model = new Appointment();
        $model->title=$data['title'];
        $model->content=isset($data['content'])? $data['content']: null;
        $model->consumer_id=isset($data['consumer_id'])? $data['consumer_id']:null;
        $model->create_time=date('Y-m-d');
		$model->service_id=1;
        $model->update_time=date('Y-m-d');
		$startStamp=strtotime($data['start']);
		if($startStamp!==false)
			$startStr=date('Y-m-d H:i:s',$startStamp);
		else $startStr=	date('Y-m-d H:i:s');
		$model->start=$startStr;
		
		$endStamp=strtotime($data['end']);
		if($endStamp!==false){
			if($endStamp<$startStamp){$endStamp=$startStamp;}
			$endStr=date('Y-m-d H:i:s',$endStamp);
		}			
		else $endStr= date('Y-m-d H:i:s');
		$model->end=$endStr;
		
		
		$model->assigned_to=$data['assigned_to'];
		$model->allDay=0;
        $model->status=1;
        if (!$model->save(false)) {
            $errors = array();
            foreach ($model->getErrors() as $e) $errors = array_merge($errors, $e);
            $this->sendResponse(500, implode("<br />", $errors));
        }

        $this->sendResponse(200,CJSON::encode($model));
    }
	
	public function actionUpdate($id)
    {
		if (null === ($model = Appointment::model()->findByPk($id)))
		throw new CHttpException(404,'Your request not found');

		
        $data = CJSON::decode(file_get_contents('php://input'));
		
		if(isset($data['title'])){
			$model->title=$data['title'];
			$model->content=isset($data['content'])? $data['content']: null;
			$model->consumer_id=isset($data['consumer_id'])? $data['consumer_id']:null;
			//$model->create_time=date('Y-m-d');
			$model->service_id=1;
			$model->update_time=date('Y-m-d');
			$model->assigned_to=$data['assigned_to'];
			$model->allDay=0;
		}
		if (isset($data['start'])){
			$startStamp=strtotime($data['start']);
			if($startStamp!==false){
				$startStr=date('Y-m-d H:i:s',$startStamp);
			}
			else $startStr=	date('Y-m-d H:i:s');
			$model->start=$startStr;
		}
		if (isset($data['end'])){
			$endStamp=strtotime($data['end']);
			if($endStamp!==false){
				if($endStamp<$startStamp){$endStamp=$startStamp;}
				$endStr=date('Y-m-d H:i:s',$endStamp);
			}
			else $endStr= date('Y-m-d H:i:s');
			$model->end=$endStr;
		}
		else {
			$endStamp=$startStamp;
			$endStr=date('Y-m-d H:i:s',$endStamp);
			$model->end=$endStr;
			}
	
		
        $model->status=1;
        if (!$model->save(false)) {
            $errors = array();
            foreach ($model->getErrors() as $e) $errors = array_merge($errors, $e);
            $this->sendResponse(500, implode("<br />", $errors));
        }
		
        $this->sendResponse(200,CJSON::encode($model));
    }
}